package com.example;

import com.example.utils.ConnectionFactoryUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * @author wangxiyuan
 * @description
 * @createDate 2021/3/26 16:33
 **/
public class AlternateExchangeDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = ConnectionFactoryUtils.getConnectionFactory();
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {

            Map<String,Object> argsMap = new HashMap<>();
            argsMap.put("alternate-exchange","test_alternate_exchange");
            channel.exchangeDeclare("direct_logs", "direct",false,false,argsMap);
            // 备用交换器
            channel.exchangeDeclare("test_alternate_exchange",BuiltinExchangeType.FANOUT,
                    true,false,null);
            // 第一个参数 队列名 第二个参数 交换机名
            channel.queueBind("hello", "test_alternate_exchange", "");
            channel.queueBind("hello_queue", "direct_logs", "xxx");
            // routing key = test_alternate
            channel.basicPublish("direct_logs", "test_alternate",
                    null,
                    ("testInfo").getBytes("UTF-8"));
        }
    }
}
